/*
 *  include/asm/i8259.h
 *
 *  last modified at 2007.07.14 21:56 by ChenJie
 *
 */

#ifndef I8259_H
#define I8259_H

#include <asm/i386.h>

#define IRQ_8259M	(0x20)
#define IRQ_8259S	(0x28)

#define IRQ_VECTOR(n)		((n<8)?(IRQ_8259M+n): (IRQ_8259S+n-8))
#define NR_IRQ				(16)

#define IRQ_CLOCK		(0+0)
#define IRQ_KEYBOARD	(0+1)
#define IRQ_IRQ2			(0+2)
#define IRQ_TTY2			(0+3)
#define IRQ_TTY1			(0+4)
#define IRQ_XTWD		(0+5)
#define IRQ_FLOPPY		(0+6)
#define IRQ_PRINTER		(0+7)

#define IRQ_REALCLOCK	(8+0)
#define IRQ_IRQ9			(8+1)
#define IRQ_IRQ10		(8+2)
#define IRQ_IRQ11		(8+3)
#define IRQ_IRQ12		(8+4)
#define IRQ_FPU			(8+5)
#define IRQ_ATWD		(8+6)
#define IRQ_IRQ15		(8+7)


#define SEND_MASTER_EOI()  outb(0x20, 0x20) 
#define SEND_SLAVE_EOI()  outb(0x20, 0xa0)

#define enable_irq_clock() \
	outb(inb(0x21)&~0x01, 0x21)

#define disable_irq_clock() \
	outb(inb(0x21)|0x01, 0x21)

#define enable_irq(irq) \
	(irq<8)? outb(inb(0x21)&~(1<<irq), 0x21) :outb(inb(0xa1)&~(1<<(irq-8)), 0xa1)
	
#define disable_irq(irq) \
	(irq<8)? outb(inb(0x21) | (1<<irq), 0x21) :outb(inb(0xa1) | (1<<(irq-8)), 0xa1)
	
#define enable_all_irq() \
	outb(0x00, 0x21)  \
	outb(0x00, 0xa1)

#define disable_all_irq() \
	outb(0xfb, 0x21)  \
	outb(0xff, 0xa1)

// i8259初始化
static inline void i8259_init(void)
{
	__asm__ __volatile__ (
		"\tmovb $0x11, %%al\n"//ICW1, 00010001B:有ICW4, 级联， 边沿触发
		"\toutb %%al, $0x20\n"//发送到8259主片偶地址
		"\tnop\n"
		"\toutb %%al, $0xa0\n"//发送到8259从片偶地址
		"\tnop\n"
		"\tmovb $0x20, %%al\n"//ICW2, 设置主片中断从0x20开始
		"\toutb %%al, $0x21\n"//发送到8259主片奇地址
		"\tnop\n"
		"\tmovb $0x28, %%al\n"//从片中断从0x28开始
		"\toutb %%al, $0xa1\n"//发送到8259从片奇地址
		"\tnop\n"
		"\tmovb $0x04, %%al\n"//ICW3, 00000100B:主片IR2引脚接从片
		"\toutb %%al, $0x21\n"//发送到8259主片奇地址
		"\tnop\n"
		"\tmovb $0x02, %%al\n"//00000010B:从片接主片IR2引脚
		"\toutb %%al, $0xa1\n"//发送到8259从片奇地址
		"\tnop\n"
		"\tmovb $0x01, %%al\n"//ICW4,00000001B:8086模式,正常EOI(非自动)，非缓冲
		"\toutb %%al, $0x21\n"//发送到8259主片奇地址
		"\tnop\n"
		"\toutb %%al, $0xa1\n"//发送到8259从处奇地址
		"\tnop\n"
		"\tmovb $0xfb, %%al\n"//OCW1, 11111011B, 屏蔽除IR2的所有中断
		"\toutb %%al, $0x21\n"//发送到主片的奇地址
		"\tnop\n"
		"\tmovb $0xff, %%al\n"//11111111B：关闭所有中断
		"\toutb %%al, $0xa1\n"//发送到从片的奇地址
		::);
}


#endif //I8259_H


